package main

type CountByExt map[string]*Count

func (cx CountByExt) Add(one *Count) {
	// 如果 cx 是 nil，则先初始化
	if cx == nil {
		cx = make(CountByExt)
	}
	r, ok := cx[one.Ext]
	if !ok {
		// 如果不存在，则初始化一个新的 Count 对象
		cx[one.Ext] = &Count{
			Ext:       one.Ext,
			FileCount: one.FileCount,
			Binary:    one.Binary,
			Blank:     one.Blank,
			Code:      one.Code,
		}
		return
	}
	// 如果存在，则累加
	r.FileCount += one.FileCount
	r.Binary += one.Binary
	r.Blank += one.Blank
	r.Code += one.Code
}

type Count struct {
	Ext       string // 文件扩展
	FileCount int    // 文件数
	Binary    int    // 二进制
	Blank     int    // 空行
	Code      int    // 代码行
}

// 总数加
func (c *Count) Add(one *Count) {
	c.Ext = one.Ext
	c.FileCount += one.FileCount
	c.Binary += one.Binary
	c.Blank += one.Blank
	c.Code += one.Code
}

type Counts []*Count

func (cs Counts) Len() int {
	return len(cs)
}

// 交换i, k位置值
func (cs Counts) Swap(i, k int) {
	cs[i], cs[k] = cs[k], cs[i]
}

type ByCode struct {
	Counts
}

// 根据代码数比较
func (bc ByCode) Less(i, k int) bool {
	a, b := bc.Counts[i], bc.Counts[k]
	if a.Code == b.Code { // 如果代码数相等
		if a.Binary == b.Binary { // 如果二进制相等
			return a.Ext < b.Ext // 扩展名短的
		}
		return a.Binary > b.Binary
	}
	return a.Code > b.Code
}

type ByExt struct {
	Counts
}

// 根据扩展名数比较
func (bx ByExt) Less(i, k int) bool {
	a, b := bx.Counts[i], bx.Counts[k]
	if a.Ext == b.Ext {
		if a.Code == b.Code {
			if a.Binary == b.Binary {
				return a.Blank < b.Blank
			}
			return a.Binary > b.Binary
		}
	}
	return a.Ext < b.Ext
}
